package com.zjff.show.zjffshow.controller;

import com.zjff.show.zjffshow.entity.ResultDao;
import com.zjff.show.zjffshow.entity.UserDao;
import com.zjff.show.zjffshow.service.UserService;
import com.zjff.show.zjffshow.tool.JSONTool;
import com.zjff.show.zjffshow.tool.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/User/")
public class UserController {

    @Autowired
    UserService userService;
    ResultDao resultDao = null;
    Jedis jedis = new RedisTool().getJedis();

    //获取用户状态
    @GetMapping("/getUser")
    private String getUser(@RequestParam("UName") String UName){
        resultDao = new ResultDao();
        resultDao.setData(userService.getUser(UName).getStatus());
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    @RequestMapping(value = "/getUserList",method = RequestMethod.GET)
    private String getUserList(HttpServletRequest request){
        resultDao = new ResultDao();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        String Pwd = request.getParameter("Pwd");
        String UName = request.getParameter("UName");
        String Phone = request.getParameter("Phone");
        String Address = request.getParameter("Address");
        int intid = 0;
        boolean flag = false;
        if (id!=null && !id.equals("") && !id.equals("0")){
            intid = Integer.parseInt(id);
            flag = true;
        }
        if (!Pwd.equals("")){
            flag = true;
        }
        if (!UName.equals("")){
            flag = true;
        }
        if (!Phone.equals("")){
            flag = true;
        }
        if (!Address.equals("")){
            flag = true;
        }
        int Start=(page-1)*limit;//设置起始位置
        List<UserDao> list = userService.getUserList(intid,Pwd,Start,limit,UName,Phone,Address);
        resultDao.setCode("0");
        resultDao.setMsg("获取成功！");
        if (flag){
            resultDao.setCount(String.valueOf(list.size()));
        }else {
            resultDao.setCount(String.valueOf(userService.getUserCount()));
        }
        resultDao.setData(list);
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    //获取所有用户数量
    @RequestMapping(value = "/getUserCount",method = RequestMethod.GET)
    private String getUserCount(HttpServletRequest request){
        resultDao = new ResultDao();
        int count = userService.getUserCount();
        resultDao.setCount(String.valueOf(count));
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    @GetMapping("/upUser")
    private String upUser(
                                @RequestParam("Pwd") String Pwd ,
                                @RequestParam("Phone") String Phone,
                                @RequestParam("Address") String Address,
                                @RequestParam("id") int id
    ){
        resultDao = new ResultDao();
        UserDao userDao = new UserDao();
        userDao.setPwd(Pwd);
        userDao.setPhone(Phone);
        userDao.setAddress(Address);
        userDao.setId(id);
        int result = userService.upUser(userDao);
        if (result>0){
            resultDao.setCode("0");
            resultDao.setMsg("更新成功");
        }else{
            resultDao.setCode("1");
            resultDao.setMsg("更新失败");
        }
        return JSONTool.getJsonTool().toJson(resultDao);
    }


}
